package com.api.edc.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.nova.edc.model.api.ReportMenu;
import com.nova.edc.model.energy.Gather;
import com.nova.edc.model.system.*;
import com.nova.edc.service.api.ReportMenuService;
import com.nova.edc.service.energy.GatherService;
import com.nova.edc.service.energy.GatherSummaryService;
import com.nova.edc.service.system.AreaPointService;
import com.nova.edc.service.system.CustomerAreaService;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.edc.service.system.GroupService;
import com.nova.frame.utils.DateUtils;
import com.nova.frame.utils.JdbcUtils;
import com.nova.frame.utils.StringUtils;
import com.seio.edc.common.Constants;
import com.seio.edc.util.DateUtil;
import com.seio.edc.util.IntegerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2016/5/27 0027.
 */
@Controller
@RequestMapping(value = "/reportapp")
public class ReportAPIController {

    @Autowired
    private ReportMenuService novaReportMenuService;
    @Autowired
    private GatherSummaryService novaGatherSummaryService;
    @Autowired
    private GroupService novaGroupService;
    @Autowired
    private CustomerInfoService novaCustomerInfoService;
    @Autowired
    private AreaPointService novaAreaPointService;
    @Autowired
    private GatherService novaGatherService;
    @Autowired
    private CustomerAreaService novaCustomerAreaService;

    @RequestMapping(value = "getMenus.shtml")
    @ResponseBody
    public List<ReportMenu> getMenus() {
        List<ReportMenu> menuList = novaReportMenuService.findAllMenuList();
        return menuList;
    }

    @RequestMapping(value = "getEnergyByDate.shtml")
    @ResponseBody
    public List<Map<String, Object>> getEnergyByDate(String startDate, String endDate, String groupName, String areaName, String storeName, int chartType) {
        String format = null;
        if (chartType == ModelConstant.API_CHARTTYPE_TIME) {
            format = ModelConstant.API_FORMAT_YYYY_MM_DD;
        } else {
            format = getFormat(startDate, endDate);
        }
        List<CustomerInfo> customerInfoList = null;
        Group group = novaGroupService.getByGroupName(groupName);
        if (StringUtils.isNotNullOrEmpty(areaName)) {
            customerInfoList = novaCustomerInfoService.findByGroupProvince(group.getGroupcode(), areaName);
        } else if (StringUtils.isNotNullOrEmpty(storeName)) {
            customerInfoList = novaCustomerInfoService.findByGroupName(group.getGroupcode(), storeName);
        } else {
            customerInfoList = novaCustomerInfoService.findByGroupcode(group.getGroupcode());
        }
        List<Map<String, Object>> list = novaGatherSummaryService.getEnergyByCompanyAPI(customerInfoList, startDate, endDate, format);
        return list;
    }

    @RequestMapping(value = "findAreaNum.shtml")
    @ResponseBody
    public List<Map<String, Object>> findAreaNum() {
        List<Map<String, Object>> list = novaCustomerInfoService.findAreaNum();
        return list;
    }

    @RequestMapping(value = "getEnergyByAreaAPI.shtml")
    @ResponseBody
    public List<Map<String, Object>> getEnergyByAreaAPI(String startDate, String endDate, String groupName, String areaName, String storeName, int areaType, int orderType) {
        List<CustomerInfo> customerInfoList = null;
        Group group = novaGroupService.getByGroupName(groupName);
        if (StringUtils.isNotNullOrEmpty(areaName)) {
            customerInfoList = novaCustomerInfoService.findByGroupProvince(group.getGroupcode(), areaName);
        } else if (StringUtils.isNotNullOrEmpty(storeName)) {
            customerInfoList = novaCustomerInfoService.findByGroupName(group.getGroupcode(), storeName);
        } else {
            customerInfoList = novaCustomerInfoService.findByGroupcode(group.getGroupcode());
        }
        String area = null;
        if (ModelConstant.API_AREATYPE_PROVINCE == areaType) {
            area = ModelConstant.API_AREA_PROVINCE;
        } else if (ModelConstant.API_AREATYPE_CITY == areaType) {
            area = ModelConstant.API_AREA_CITY;
        } else if (ModelConstant.API_AREATYPE_DISTRICT == areaType) {
            area = ModelConstant.API_AREA_DISTRICT;
        } else if (ModelConstant.API_AREATYPE_STORE == areaType) {
            area = ModelConstant.API_AREA_STORE;
        } else {
            area = ModelConstant.API_AREA_AREA;
        }
        List<Map<String, Object>> list = novaGatherSummaryService.getEnergyByAreaAPI(customerInfoList, startDate, endDate, area, orderType);
        return list;
    }

    @RequestMapping(value = "findStores.shtml")
    @ResponseBody
    public List<String> findStores(String groupName) {
        Group group = novaGroupService.getByGroupName(groupName);
        List<CustomerInfo> customerInfos = novaCustomerInfoService.findByGroupcode(group.getGroupcode());
        List<String> Stores = new ArrayList<String>();
        for (int i = 0; i < customerInfos.size(); i++) {
            Stores.add(customerInfos.get(i).getCustomername());
        }
        return Stores;
    }

    @RequestMapping(value = "/areaState.shtml")
    @ResponseBody
    public List<Gather> areaState(HttpServletRequest request,
                                  HttpSession session, ModelMap modelMap, String storeName) {
        List<Gather> gatherList = null;
        int id = novaCustomerInfoService.findIdByName(storeName);
        List<AreaPoint> points = novaAreaPointService.findAreaMainPoint(id, "仓库");
        if (points == null || points.size() == 0) {
            return gatherList;
        } else {
            AreaPoint point = novaAreaPointService.findById(points.get(0).getId());
            String endDate = DateUtils.getDate2SStr(new Date());
            JdbcUtils.changeDataSource(Constants.COLLECT_DB);
            gatherList = novaGatherService.findOneToday(endDate + " 00:00:00", endDate + " 23:59:59", point, ModelConstant.GATHER_ERRORSTATE);
            JdbcUtils.changeDataSource(Constants.DEFAULT_DB);
            List<Gather> gathers = novaGatherService.disposeforchart(gatherList, endDate);
            return gathers;
        }
    }

    @RequestMapping(value = "/areaRealEnergy.shtml")
    @ResponseBody
    public List<CustomerArea> areaRealEnergy(HttpServletRequest request,
                                             HttpSession session, ModelMap modelMap, String storeName) {
        int id = novaCustomerInfoService.findIdByName(storeName);
        List<AreaPoint> areaPoints = novaAreaPointService.findAreaMainPoint(id, "");
        String date = DateUtils.getDate2SStr(new Date());
        JdbcUtils.changeDataSource(Constants.COLLECT_DB);
        for (AreaPoint point : areaPoints) {
            List<Gather> oneToday = novaGatherService.findOneToday(date + " 00:00:00", date + " 23:59:59", point, ModelConstant.LIGHT_ON);
            int summaryTime = 0;
            for (Gather gather : oneToday) {
                if (gather.getActivestate() == ModelConstant.LIGHT_ON) {
                    summaryTime = summaryTime + gather.getActivetime();
                }
            }
            point.setUseminute(summaryTime);
        }
        JdbcUtils.changeDataSource(Constants.DEFAULT_DB);
        List<CustomerArea> customerAreaList = novaCustomerAreaService.findByCustomereInfoIdAPI(id);
        for (CustomerArea area : customerAreaList) {
            double power = 0.0000;
            for (AreaPoint point : areaPoints) {
                if (point.getAreaid() - area.getId() == 0) {
                    BigDecimal bigDecimal = new BigDecimal(point.getUseminute() * point.getRealpower());
                    BigDecimal bigDecimal1 = new BigDecimal(60000);
                    double value = bigDecimal.divide(bigDecimal1).doubleValue();
                    power = power + value;
                }
            }
            area.setEnergy(power);
        }
        return customerAreaList;

    }

    public String getFormat(String startDate, String endDate) {
        int endMonth = DateUtil.getMonth(DateUtils.getStr2SDate(endDate));
        int startMonth = DateUtil.getMonth(DateUtils.getStr2SDate(startDate));
        if (endMonth - startMonth > 0) {
            return ModelConstant.API_FORMAT_YYYY_MM;
        } else {
            return ModelConstant.API_FORMAT_DD;
        }
    }

}
